﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using Caliburn.Micro;
using JY.IOC;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Extensions;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.Tools;
using JY.Wpf.Core.ViewModelBases;

namespace JY.Wpf.TestClient.ViewModels
{
    public class LeftMenuViewModel : ViewModelBase
    {
        #region # 变量
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public LeftMenuViewModel()
        {
            this.InitView();
        }
        #endregion

        #region # 属性
        #region # 系统菜单——ObservableCollection<SystemMenu> SystemMenus

        /// <summary>
        /// 系统菜单
        /// </summary>
        public ObservableCollection<SystemMenu> SystemMenus { get; set; }

        #endregion

        #region # 当前菜单
        /// <summary>
        /// 当前菜单
        /// </summary>
        public SystemMenu CurSystemMenu { get; set; }
        #endregion
        #endregion

        #region # Command方法

        #region # 用户信息——void UserInfo()
        /// <summary>
        /// 用户信息
        /// </summary>
        public void UserInfo()
        {
            //this.EventAggregator.PublishOnUIThread(new ShowChildWindowEvent(new LoginViewModel("立高智造", "")));
        }
        #endregion

        #region # 消息——void Message()
        /// <summary>
        /// 消息
        /// </summary>
        public void Message()
        {
            this.EventAggregator.PublishOnUIThreadAsync(new ShowWindowEvent(new TestWindowViewModel()));
            //MessageBox.Show("消息");
        }
        #endregion

        #region # 工具——void Tool()
        /// <summary>
        /// 工具
        /// </summary>
        public void Tool()
        {
            //MessageBox.Show("工具");
        }
        #endregion

        #region # 设置——void Setting()
        /// <summary>
        /// 设置
        /// </summary>
        public async void Setting()
        {
            await this.EventAggregator.PublishOnUIThreadAsync(new BusyIndicatorEvent(true, "执行中..."));
            await Task.Delay(3000);
            await this.EventAggregator.PublishOnUIThreadAsync(new BusyIndicatorEvent(false));

            await this.EventAggregator.PublishOnUIThreadAsync(new ShowChildWindowEvent(new TestChildWindowViewModel()));
        }
        #endregion

        #region # 退出登录——void LogOut()
        /// <summary>
        /// 退出登录
        /// </summary>
        public void LogOut()
        {
            //MessageBox.Show("退出登录");
        }
        #endregion

        #region 跳转页面——void Navigate(SystemMenu systemMenu)
        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="systemMenu">菜单项</param>
        public void SelectedMainMenu(SystemMenu systemMenu)
        {
            if (systemMenu.SubMenus.Any())
            {
                this.CurSystemMenu = systemMenu;
            }
            else
            {
                this.CurSystemMenu = null;
                this.Navigate(systemMenu);
            }
        }
        #endregion

        #region 跳转页面——void Navigate(SystemMenu systemMenu)
        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="systemMenu">菜单项</param>
        public void Navigate(SystemMenu systemMenu)
        {
            if (string.IsNullOrWhiteSpace(systemMenu.Url))
            {
                this.EventAggregator.PublishOnUIThreadAsync(new WarningMessageEvent("当前菜单权限不正确！"));
                return;
            }

            Type type = systemMenu.Url.GetPathType();
            if (type == null)
            {
                this.EventAggregator.PublishOnUIThreadAsync(new WarningMessageEvent("当前菜单权限不正确！"));
                return;
            }
            this.CurSystemMenu = null;
            object content = ResolveMediator.Resolve(type);
            if (content is WindowViewModelBase windowViewModel)
            {
                windowViewModel.Title = systemMenu.Name;
                this.EventAggregator.PublishOnUIThreadAsync(new ShowWindowEvent(windowViewModel));
            }
            else if (content is ChildWindowViewModelBase childWindowViewModel)
            {
                childWindowViewModel.Title = systemMenu.Name;
                this.EventAggregator.PublishOnUIThreadAsync(new ShowChildWindowEvent(childWindowViewModel,false));
            }
            else if (content is ViewModelBase viewModel)
            {
                viewModel.Title = systemMenu.Name;
                this.EventAggregator.PublishOnUIThreadAsync(new SetContentEvent(viewModel));
            }
        }
        #endregion

        #region # 关闭子菜单——void CloseSubMenu()
        /// <summary>
        /// 关闭子菜单
        /// </summary>
        public void CloseSubMenu()
        {
            this.CurSystemMenu = null;
        }
        #endregion
        #endregion

        #region # 方法

        #region # 初始化页面——void InitView()
        /// <summary>
        /// 初始化页面
        /// </summary>
        public void InitView()
        {
            List<string> brushStrList = new List<string>()
            {
                "WarningBrush",
                "InfoBrush",
                "SuccessBrush",
                "AccentBrush",
                "DangerBrush",
                "DarkWarningBrush",
                "DarkInfoBrush",
                "DarkSuccessBrush",
                "DarkAccentBrush",
                "DarkDangerBrush",
            };
            Random random = new Random();
            var packIconMaterialKinds=Enum.GetNames(typeof(MahApps.Metro.IconPacks.PackIconMaterialKind)).ToList();
            packIconMaterialKinds.Remove(MahApps.Metro.IconPacks.PackIconMaterialKind.None.ToString());
            ObservableCollection<SystemMenu> caliburnMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("Bootstrapper","JY.Wpf.TestClient.ViewModels.CaliburnDemoViewModels.BootstrapperViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("Actions","JY.Wpf.TestClient.ViewModels.CaliburnDemoViewModels.ActionsViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("Screens","JY.Wpf.TestClient.ViewModels.CaliburnDemoViewModels.ScreensViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("EventAggregator","JY.Wpf.TestClient.ViewModels.CaliburnDemoViewModels.EventAggregatorViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},

            };
            ObservableCollection<SystemMenu> controlMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("变更皮肤样式","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.ChangeSkinViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("头像","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.GravatarDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("按钮","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.ButtonsViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("对话气泡","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.ChatBoxDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("输入控件","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.InputDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("日期时间选择器","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.DateDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("搜索栏","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.SearchDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("标签","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.TagDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("封面视图","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.CoverViewDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("进度条","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.ProgressBarViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("等待遮罩层","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.BusyIndicatorDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]),
                new SystemMenu("滚动块","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.RunningBlockDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("图片浏览器","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.ImageBrowserDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("时间条","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.TimeBarViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("卡片","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.CardDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("桌面通知","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.NotificationDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("抽屉弹层","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.DrawerDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("边框","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.BorderDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("选项卡控件","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.TabControlDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("树形列表控件","JY.Wpf.TestClient.ViewModels.ControlDemoViewModels.TreeListViewDemoViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
            };
            ObservableCollection<SystemMenu> MVVMMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("框架说明","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.InstructionsViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("编码约定","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.ConventionsViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                //new SystemMenu("MVVM基类","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.BasesViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]),
                //new SystemMenu("项目引用&配置","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.InstructionsViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]),
                new SystemMenu("弹出窗口","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.PopupWinodwViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("弹出子窗口","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.ChildWinodwViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("警报消息","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.WarningMessageViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("消息提示","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.MessageViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("授权测试","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.LicenseViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("分页列表","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.PageTestViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("称量控件","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.BalanceTestViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("翻转控件","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.FlipperTestViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("储位示例","JY.Wpf.TestClient.ViewModels.MVVMDemoViewModels.StorageAreaTestViewModel",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
            };
            ObservableCollection<SystemMenu> kanbanMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("Scada看板1","",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
                new SystemMenu("Scada看板2","",packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){ColorBrushStr = brushStrList[random.Next(0, brushStrList.Count-1)]},
            };
            this.SystemMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("Caliburn.MicroDemo",null,packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){SubMenus = caliburnMenus},
                new SystemMenu("控件Demo",null,packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){SubMenus = controlMenus},
                new SystemMenu("MVVM & 主页面Demo",null,packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){SubMenus = MVVMMenus},
                new SystemMenu("Scada看板Demo",null,packIconMaterialKinds[random.Next(0, packIconMaterialKinds.Count-1)]){SubMenus = kanbanMenus},
            };
        }
        #endregion

        #region # 获取资源类型——Type GetUrlType(string url)
        /// <summary>
        /// 获取资源类型
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns></returns>
        public static Type GetUrlType(string url)
        {
            try
            {
                var assemblyUrlStr = url.Substring(0, url.IndexOf(".", StringComparison.Ordinal));
                Assembly assembly = Application.Current.GetType().Assembly;
                var assemblyStr = assembly.FullName.Substring(0, assembly.FullName.IndexOf(",", StringComparison.Ordinal));
                if (assemblyStr == assemblyUrlStr)
                {
                    return Type.GetType(url);
                }
                else
                {
                    return Assembly.Load(assemblyUrlStr).ExportedTypes.FirstOrDefault(x => x.ToString() == url);
                }
            }
            catch (Exception)
            {
                return Type.GetType(url);
            }
        }
        #endregion


        #endregion
    }
}
